package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.storage.StorageManageMatrMoveAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameItemBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrMoveActivity extends BaseActivity {
    private TextView title;


    private ListView listView;

    private StorageManageMatrMoveAdapter adapter;

    private StorageManageMatrBean.DataBean.MaterielListBean item;
    private String idStr;



    private StorageManageMatrBean bean;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_matr_move);
        initView(this);
        initData(this);
    }


    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (ListView) activity.findViewById(R.id.list);

    }

    private void initData(Activity activity) {
        title.setText("仓位信息");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_matr_move_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //保存sp
                SPUtils.saveInformation(activity,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION,bean);

                finish();
            }
        });

        setmScanListener((String scanResult, int length) -> {
            String position = QRCodeUtils.DecryptQRCode(scanResult);
            EditText pos = (EditText)listView.findViewById(R.id.position);
            pos.setText(position);
        },  QRCodeConstant.TYPE_POSITION);

        getData(activity);
    }

    private void getData(Activity activity) {
        idStr = activity.getIntent().getStringExtra(StorageManageExtra.ID);
        item = findItemBy(idStr);



        List<UpFrameItemBean.DataBean.ListBean> list = item.getUpList();

        if(adapter==null){
            this.adapter = new StorageManageMatrMoveAdapter(this,item);
            this.listView.setAdapter(adapter);
        }else{
            this.adapter.setList(list);
            this.adapter.notifyDataSetChanged();
        }



    }

    private StorageManageMatrBean.DataBean.MaterielListBean findItemBy(String id) {
        bean = SPUtils.getInformation(this,StorageManageMatrBean.class,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);
        List<StorageManageMatrBean.DataBean.MaterielListBean> ls = bean.getData().getMaterielList();
        for(int i=0;i<ls.size();i++){
            if(ls.get(i).getId().equals(id)){
                return ls.get(i);
            }
        }
        return null;

    }


    @Override
    public void onBackPressed() {
        //保存sp
        SPUtils.saveInformation(this,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION,bean);
        finish();
    }
}
